package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResponse;
import com.heima.entity.TbRole;
import com.heima.entity.TbUser;
import com.heima.mapper.TbUserMapper;
import com.heima.pojo.vo.TbUserVo;
import com.heima.service.ITbPartnerService;
import com.heima.service.ITbRoleService;
import com.heima.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-09
 */
@RestController
@RequestMapping("/api/user-service")
public class TbUserController {
    @Autowired
    private ITbUserService iTbUserService;

    @Autowired
    private ITbRoleService iTbRoleService;

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private ITbPartnerService tbPartnerService;

    @GetMapping("/user/search")
    public PageResponse<TbUserVo> selectUser(@RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                             @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                             @RequestParam(value = "userName",required = false) String userName){
        Page<TbUser> page = new Page<>(pageIndex,pageSize);

        //构建查询参数
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        if (userName != null) {
            wrapper.like("user_name",userName);
        }
        //执行分页查询
        iTbUserService.page(page,wrapper);

        List<TbUserVo> userVoList = page.getRecords().stream().map(r -> {
            TbUserVo tbUserVo = new TbUserVo();
            Integer roleId = r.getRoleId();
            TbRole role = iTbRoleService.getById(roleId);
            BeanUtil.copyProperties(r, tbUserVo);
            tbUserVo.setRole(role);
            return tbUserVo;
        }).collect(Collectors.toList());

        //封装并返回结果
        PageResponse<TbUserVo> response = new PageResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount((int) page.getTotal());
        response.setCurrentPageRecords(userVoList);

        return response;
    }
    @GetMapping("/role")
    public List<TbRole> roleList(){
        List<TbRole> roleList = iTbRoleService.list();
        return roleList;
    }
    @PostMapping("/user")
    public Boolean addUser(@RequestBody TbUser tbUser){
        TbUser tbUser1 = new TbUser();
        tbUser1.setUserName(tbUser.getUserName());
        tbUser1.setRoleId(tbUser.getRoleId());
        tbUser1.setMobile(tbUser.getMobile());
        tbUser1.setRegionId(tbUser.getRegionId());
        tbUser1.setRegionName(tbUser.getRegionName());
        tbUser1.setStatus(tbUser.getStatus());
        tbUser1.setImage(tbUser.getImage());
        int row = tbUserMapper.insert(tbUser1);
        if (row > 0) {
            return true;
        }
        return false;
    }

    @PutMapping("/user/{userId}")
    public Boolean updateUser(@PathVariable Integer userId,
                              @RequestBody TbUser tbUser){
        TbUser tbUser1 = new TbUser();
        tbUser1.setId(userId);
        BeanUtil.copyProperties(tbUser,tbUser1);
        int row = tbUserMapper.updateById(tbUser1);
        if (row >0) {
            return true;
        }
        return false;
    }
    @DeleteMapping("/user/{userId}")
    public Boolean deleteUser(@PathVariable("userId") Integer userId){
        boolean flag = iTbUserService.removeById(userId);
        return flag;
    }

    @GetMapping("/partner/count")
    public Integer PartnerCount(){
        Integer count = tbPartnerService.lambdaQuery()
                .count();
        return count;
    }
}
